<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\cache\credit\ICreditCache;
use app\common\cache\credit\ICreditRecommendCache;
use app\common\controller\AdminBaseController;
use app\common\enum\CreditStatus;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class CreditRecommend extends AdminBaseController{

    /**
     * @功能：信用卡推荐
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_list(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id");
        $credit_recommend_location = IRequest::get("credit_recommend_location");
        $area = IRequest::get("area");

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0];
            if(!empty($area[1])) $city_id = $area[1];
        }

        //判断城市是否正确
        $area_where = null;
        if(!empty($province_id) && !empty($city_id)){
            $area_where = " and ((province is null and city is null) or (province='{$province_id}' and city is null) or (province='{$province_id}' and city ='{$city_id}'))";
        }else{
            if(!empty($province_id)){
                $area_where = " and (province is null or province='{$province_id}')";
            }
        }

        //设置查询条件
        $is_delete = IsDelete::No;
        $credit_status = CreditStatus::release;
        $credit_recommend_where['cr.is_delete'] = IsDelete::No;
        if(!empty($credit_product_id)) $credit_recommend_where['cr.credit_product_id'] = $credit_product_id;
        if(!empty($credit_recommend_location)) $credit_recommend_where['cr.credit_recommend_location'] = $credit_recommend_location;

        //取得数量
        $credit_recommend_info = IDb::getInstance('credit_recommend as cr')
            ->setDbFiled(array("count(distinct cr.credit_recommend_id) as num"))
            ->setDbJoin("credit_product as ct","ct.is_delete='{$is_delete}' and ct.credit_status='{$credit_status}' and cr.credit_product_id=ct.credit_product_id")
            ->setDbJoin("credit_product_area as cpa","ct.credit_product_id = cpa.credit_product_id {$area_where}")
            ->setDbWhere($credit_recommend_where)
            ->row();

        //取得数据
        $credit_recommend_count = intval($credit_recommend_info['num']);
        if($credit_recommend_count > 0) {
            //实例化对象
            $credit_recommend_list = IDb::getInstance('credit_recommend as cr')
                ->setDbFiled("distinct cr.*")
                ->setDbJoin("credit_product as ct", "ct.is_delete='{$is_delete}' and ct.credit_status='{$credit_status}' and cr.credit_product_id=ct.credit_product_id")
                ->setDbJoin("credit_product_area as cpa", "ct.credit_product_id = cpa.credit_product_id {$area_where}")
                ->setDbWhere($credit_recommend_where)
                ->setDbOrder("cr.order_by desc,cr.credit_recommend_id desc")
                ->pag(20,$credit_recommend_count);
        }else{
            $credit_recommend_list = array();
        }

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_recommend_location",$credit_recommend_location);
        $this->assign("credit_recommend_list",$credit_recommend_list);
        $this->assign("area_list",$area_list);
        $this->assign("area",$area);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡推荐
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_del_list(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id");
        $credit_recommend_location = IRequest::get("credit_recommend_location");

        //设置查询条件
        $where['is_delete'] = IsDelete::Yes;
        if(!empty($credit_product_id)) $where['credit_product_id'] = $credit_product_id;
        if(!empty($credit_recommend_location)) $where['credit_recommend_location'] = $credit_recommend_location;

        //取得信用卡数据
        $credit_recommend_list = IDb::getInstance("credit_recommend")
            ->setDbWhere($where)
            ->setDbOrder("credit_recommend_location,credit_recommend_id")
            ->pag();

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_recommend_location",$credit_recommend_location);
        $this->assign("credit_recommend_list",$credit_recommend_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡推荐增加
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_add(){
        //取得信用卡列表
        $credit_product_where['credit_status'] = CreditStatus::release;
        $credit_product_where['is_delete'] = IsDelete::No;
        $credit_list = IDb::getInstance('credit_product')->setDbWhere($credit_product_where)->sel();

        //页面赋值
        $this->assign("credit_list",$credit_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡推荐编辑
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_edit(){
        //接收参数
        $credit_recommend_id = IRequest::get("credit_recommend_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得推荐详情
        $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
        $credit_recommend_info = IDb::getInstance("credit_recommend")->setDbWhere($credit_recommend_where)->row();

        //页面赋值
        $this->assign("credit_recommend_id",$credit_recommend_id);
        $this->assign("credit_recommend_info",$credit_recommend_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡推荐
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_submit(){
        //接收参数
        $credit_recommend_id = IRequest::get("credit_recommend_id");
        $credit_recommend_location = IRequest::get("credit_recommend_location",IRequest::NOT_EMPTY,"信用卡推荐位置编号不能为空！");
        $credit_note = IRequest::get("credit_note");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        //查看是否存在历史数据
        if(empty($credit_recommend_id)){
            //接收参数
            $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

            //修改数据
            $credit_recommend_data['credit_product_id'] = $credit_product_id;
            $credit_recommend_data['credit_recommend_location'] = $credit_recommend_location;
            $credit_recommend_data['credit_note'] = $credit_note;
            $credit_recommend_data['order_by'] = $order_by;
            $credit_recommend_data['remark'] = $remark;
            $credit_recommend_data['create_admin'] = get_login_admin_id();
            $credit_recommend_data['create_time'] = get_date_time();
            $credit_recommend_add = IDb::getInstance('credit_recommend')->setDbData($credit_recommend_data)->add();
            if($credit_recommend_add == false){
                error("推荐失败！");
            }
        }else{
            //修改数据
            $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
            $credit_recommend_data['credit_recommend_location'] = $credit_recommend_location;
            $credit_recommend_data['credit_note'] = $credit_note;
            $credit_recommend_data['order_by'] = $order_by;
            $credit_recommend_data['remark'] = $remark;
            $credit_recommend_data['update_admin'] = get_login_admin_id();
            $credit_recommend_data['update_time'] = get_date_time();
            $credit_tag_upd = IDb::getInstance('credit_recommend')->setDbWhere($credit_recommend_where)->setDbData($credit_recommend_data)->upd();
            if($credit_tag_upd == false){
                error("推荐失败！");
            }
        }

        //删除缓存信息
        ICreditRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！",Url("credit_recommend_list"));
    }

    /**
     * @功能：信用卡推荐删除
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_del(){
        //接收参数
        $credit_recommend_id= IRequest::get("credit_recommend_id",IRequest::NOT_EMPTY,"信用卡推荐编号不能为空！");

        //修改管理员信息
        $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
        $credit_recommend_data['is_delete'] = IsDelete::Yes;
        $credit_recommend_data['update_admin'] = get_login_admin_id();
        $credit_recommend_data['update_time'] = get_date_time();
        $credit_recommend_upd = IDb::getInstance('credit_recommend')->setDbWhere($credit_recommend_where)->setDbData($credit_recommend_data)->upd();
        if($credit_recommend_upd === false){
            error("信用卡推荐删除失败！");
        }

        //删除缓存信息
        ICreditRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信用卡推荐还原
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_recycle(){
        //接收参数
        $credit_recommend_id= IRequest::get("credit_recommend_id",IRequest::NOT_EMPTY,"信用卡推荐编号不能为空！");

        //取得推荐的信用卡
        $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
        $credit_recommend_info = IDb::getInstance("credit_recommend")->setDbWhere($credit_recommend_where)->row();
        if($credit_recommend_info === false){
            error("信用卡推荐还原失败！");
        }

        //取得信用卡编号
        if(empty($credit_recommend_info['credit_product_id'])){
            error("信用卡推荐还原失败！");
        }

        //取得信用卡信息
        $credit_info = ICreditCache::getInfo($credit_recommend_info['credit_product_id']);
        if($credit_info === false){
            error("信用卡推荐还原失败！");
        }

        //判断信用卡状态
        if(empty($credit_info['credit_status']) || $credit_info['credit_status'] != CreditStatus::release){
            error("信用卡不是发布状态不能推荐！");
        }

        //修改管理员信息
        $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
        $credit_recommend_data['is_delete'] = IsDelete::No;
        $credit_recommend_data['update_admin'] = get_login_admin_id();
        $credit_recommend_data['update_time'] = get_date_time();
        $credit_recommend_upd = IDb::getInstance('credit_recommend')->setDbWhere($credit_recommend_where)->setDbData($credit_recommend_data)->upd();
        if($credit_recommend_upd === false){
            error("信用卡推荐还原失败！");
        }

        //删除缓存信息
        ICreditRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：取得信用卡详情
     * @开发者：cxl
     * @return string
     */
    public function credit_recommend_info(){
        //接收参数
        $credit_recommend_id= IRequest::get("credit_recommend_id",IRequest::NOT_EMPTY,"信用卡推荐编号不能为空！");

        //取得推荐信息
        $credit_recommend_where['credit_recommend_id'] = $credit_recommend_id;
        $credit_recommend_info = IDb::getInstance("credit_recommend")->setDbWhere($credit_recommend_where)->row();
        if($credit_recommend_info === false){
            error("取得信用卡信息失败！");
        }

        //取得信用卡信息
        $credit_product_info = ICreditCache::getInfo($credit_recommend_info['credit_product_id']);
        if($credit_product_info === false){
            error("取得信用卡信息失败！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_recommend_info",$credit_recommend_info);
        $this->assign("credit_product_info",$credit_product_info);

        //渲染模板输出
        return view();
    }
}
